﻿using Microsoft.EntityFrameworkCore;
using NewRetail.Application.Components.Goods.Entity;
using NewRetail.Application.Core.Entity;

namespace NewRetail.Application.Components.Goods
{
    public static class GoodsExtension
    {
        public static void ConfigureGoodsEntities(this ModelBuilder modelBuilder)
        {
            //商品分类
            var goodsCategoryBuilder = modelBuilder.Entity<GoodsCategory>().ConfigBaseEntity().ConfigNo();
            goodsCategoryBuilder.ToTable("goods_category");
            goodsCategoryBuilder.Property(x => x.categoryName).IsRequired().HasMaxLength(50);
            goodsCategoryBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            goodsCategoryBuilder.Property(x => x.pID).HasMaxLength(50);
            goodsCategoryBuilder.Property(x => x.salePercentage).HasPrecision(18, 4);
            goodsCategoryBuilder.Property(x => x.IsDeleted).HasDefaultValue(false);

            //商品调价单
            var goodsAdjustPriceBuilder = modelBuilder.Entity<GoodsAdjustPrice>()
                .ConfigBaseEntity()
                .ConfigReceiptEntity()
                .ConfigWarehouse();

            goodsAdjustPriceBuilder.ToTable("goods_adjustPrice");
            goodsAdjustPriceBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            goodsAdjustPriceBuilder.Property(x => x.oldPrice).HasPrecision(18, 4);
            goodsAdjustPriceBuilder.Property(x => x.newPrice).HasPrecision(18, 4);
            goodsAdjustPriceBuilder.Property(x => x.remark).HasMaxLength(1000);
            goodsAdjustPriceBuilder.Ignore(x => x.GoodsInfo).Ignore(x => x.Warehouse);

            //商品表
            var infoBuilder = modelBuilder.Entity<GoodsInfo>().ConfigBaseEntity().ConfigNo();
            infoBuilder.ToTable("goods_info");
            infoBuilder.Property(x => x.goodsName).IsRequired().HasMaxLength(100);
            infoBuilder.Property(x => x.No).IsRequired().HasMaxLength(50);
            infoBuilder.Property(x => x.barCode).IsRequired().HasMaxLength(50);
            infoBuilder.Property(x => x.categoryID).IsRequired().HasMaxLength(50);
            infoBuilder.Property(x => x.brand);
            infoBuilder.Property(x => x.specifications).HasMaxLength(50);
            infoBuilder.Property(x => x.saleType);
            infoBuilder.Property(x => x.metering);
            infoBuilder.Property(x => x.unit);
            infoBuilder.Property(x => x.originPlace).HasMaxLength(200);
            infoBuilder.Property(x => x.qualityTime);
            infoBuilder.Property(x => x.price).HasPrecision(18, 4);
            infoBuilder.Property(x => x.imgUrl).HasMaxLength(200);
            infoBuilder.Property(x => x.IsDeleted).HasDefaultValue(false);
            infoBuilder.Property(x => x.colourName).IsRequired().HasMaxLength(50);
            infoBuilder.Property(x => x.sizeStName).IsRequired().HasMaxLength(50);
            infoBuilder.Property(x => x.sizeName).IsRequired().HasMaxLength(50);
            infoBuilder.Property(x => x.goodsCode).HasMaxLength(50);
            infoBuilder.Property(x => x.goodsMstId).IsRequired().HasMaxLength(50);

            infoBuilder.Ignore(x => x.GoodsPrices);
            infoBuilder.Ignore(x => x.InOutWarehousings);
            //add by cl 20221112
            infoBuilder.Ignore(x => x.GoodsCategory);

            //商品类别退货周期表
            var gcReturnCycleBuilder = modelBuilder.Entity<GoodsCategoryReturnCycle>().ConfigBaseEntity();
            gcReturnCycleBuilder.ToTable("goods_category_return_cycle");
            gcReturnCycleBuilder.Property(x => x.categoryId).IsRequired().HasMaxLength(50);
            gcReturnCycleBuilder.Property(x => x.cycleDay);
            gcReturnCycleBuilder.Property(x => x.IsDeleted).HasDefaultValue(false);
            gcReturnCycleBuilder.Ignore(x => x.GoodsCategory);
            gcReturnCycleBuilder.Ignore(x => x.Supplier);

            //商品会员价表
            var goodsMemberPriceBuilder = modelBuilder.Entity<GoodsMemberPrice>()
                .ConfigBaseEntity()
                .ConfigWarehouse();
            goodsMemberPriceBuilder.ToTable("goods_memberPrice");
            goodsMemberPriceBuilder.Property(x => x.goodsID).IsRequired().HasMaxLength(50);
            goodsMemberPriceBuilder.Property(x => x.warehouseID).IsRequired().HasMaxLength(50);
            goodsMemberPriceBuilder.Property(x => x.oldPrice).HasPrecision(18, 4);
            goodsMemberPriceBuilder.Property(x => x.memberPrice).HasPrecision(18, 4);
            goodsMemberPriceBuilder.Property(x => x.remark).HasMaxLength(1000);
            goodsMemberPriceBuilder.Ignore(x => x.GoodsInfo).Ignore(x => x.Warehouse);
        }
    }
}
